package org.gruposp2p.proyectokarma.controller

import grails.converters.JSON
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.Authentication;
import org.gruposp2p.proyectokarma.domain.Tag;

class TagController {
	
	def springSecurityService

	def show = { 
		def tagList = []
		def tagsMap = new HashMap()
		if (params.ids?.size() > 0) {
			Tag.getAll(params.ids).collect {tag ->
				if (tag) tagList << tag;
			}
			if (tagList.size() == 0) {
				response.status = 404 //Not Found
				render message(code: 'proyectokarma.tag.tagNotFound', args:[params.ids])
				return
			}
		} else {
		log.debug "params: " + params
			tagList =  Tag.list(params)
			tagsMap.numberOfTagsInSystem = Tag.count()
			tagsMap.numberOfTagsInRequest = tagList.size()
			tagsMap.offset = params.offset
		}
		tagsMap.tags = tagList.collect {tag ->
			return [id: tag.id, name: tag.name, frequency:tag.frequency]
		}
		render tagsMap as JSON
	}
	
	def delete = {
		if (params.id) {
			def tag = Tag.get(params.id)
			if (tag) tag.delete()
			else {
				response.status = 404;
				render message(code: 'proyectokarma.tag.deleteNotFound', args:[params.id])
				return
			}
			response.status = 200;
			render message(code: 'proyectokarma.tag.deleteOK', args:[params.id])
			return
		} else {
			response.status = 400  //Bad Request
			render message(code: 'proyectokarma.tag.deleteBadRequest')
			return;
		}
	}


}
